package tree;

import pojo.TreeNode;

public class MirrorTreeUtils {


    /**
     * 功能描述: 镜面树
     *
     * @param root
     * @return boolean
     */
    public static boolean isMirrorTree(TreeNode root){
        // 如果是空 树,直接返回null
        if (root == null){
            return true;
        }

        return isMirror(root,root);
    }

    /**
     * 功能描述: 是镜面树
     *
     * @param root  右子树的跟
     * @param root2 左子树的跟
     * @return boolean
     */
    private static boolean isMirror(TreeNode root,
                                    TreeNode root2) {
        if (root == null ^ root2 ==null){
            return false;
        }

        if (root == null && root2 == null  ){
            return true;
        }

        return root.equals(root2) && isMirror(root.left,root2.right) && isMirror(root.right,root2.left);
    }


    public static void main(String[] args) {
        int maxValue =10;
        int maxHeight = 5;
        TreeNode root = generateMirrorTree();
        TreeNode nonMirrorRoot = generateNonMirrorTree();

        System.out.println(isMirrorTree(root));
        System.out.println(isMirrorTree(nonMirrorRoot));
    }

    private static TreeNode generateNonMirrorTree() {
        TreeNode treeNode = new TreeNode(1,
                                         "1");
        treeNode.left = new TreeNode(2,
                                     "2");
        treeNode.right = new TreeNode(2,
                                      "2");

        treeNode.left.left = new TreeNode(4,
                                          "4");

        treeNode.left.right = new TreeNode(5,
                                           "5");

        treeNode.right.left = new TreeNode(5,
                                           "5");
        treeNode.right.right = new TreeNode(4,
                                            "14");

        return treeNode;
    }

    private static TreeNode generateMirrorTree() {
        TreeNode treeNode = new TreeNode(1,
                                         "1");
        treeNode.left = new TreeNode(2,
                                     "2");
        treeNode.right = new TreeNode(2,
                                      "2");

        treeNode.left.left = new TreeNode(4,
                                          "4");

        treeNode.left.right = new TreeNode(5,
                                           "5");

        treeNode.right.left = new TreeNode(5,
                                           "5");
        treeNode.right.right = new TreeNode(4,
                                           "4");

        return treeNode;
    }
}
